package com.zimmor.routing;

import com.rabbitmq.client.*;
import com.zimmor.util.RabbitmqUtil;

import java.io.IOException;

/**
 * @Author:zimmor
 * @Date: 2021/12/29 16:06
 * @Description 路由模式 消费者
 * @version: 1.0.0
 */

public class Customer1 {
    public static void main(String[] args) {
        //获取连接和通道
        Connection connection = RabbitmqUtil.getConnection();
        try {
            Channel channel = connection.createChannel();
            //声明交换机 type direct
            String exchange = "routing";
            channel.exchangeDeclare(exchange, BuiltinExchangeType.DIRECT);
            //获取到衣蛾临时队列
            String queue = channel.queueDeclare().getQueue();
            //把队列绑定给交换机
            String routingKey = "user.add";
            channel.queueBind(queue, exchange, routingKey);
            //消费消息
            channel.basicConsume(queue, true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者1接收到消息：" + new String(body));
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
